<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/14
 * Time: 11:02
 */

namespace app\admin\builder;

class AdminConfigBuilder extends AdminBuilder
{
    private $_title;
    private $_formsubmit; //form表单别名 用于说明该表单信息作用
    private $_suggest;
    private $_keyList = array();
    private $_data = array();
    private $_buttonList = array();
    private $_savePostUrl = '';
    private $_group = array();

    /**
     * 页面标题
     * @param $title
     * @return $this
     */
    public function title($title)
    {
        $this->_title = $title;
        return $this;
    }

    /**
     * 用于表单描述文字
     * @param $formsubmit
     * @return $this
     */
    public function formsubmit($formsubmit){
        $this->_formsubmit = $formsubmit;
        return $this;
    }

    /**
     * suggest  页面副标题
     * @param $suggest
     * @return $this
     */
    public function suggest($suggest)
    {
        $this->_suggest = $suggest;
        return $this;
    }

    /**
     * 键，一般用于内部调用
     * @param      $name
     * @param      $title
     * @param null $subtitle
     * @param      $type
     * @param null $opt
     * @return $this
     */
    private function key($name, $title=null, $subtitle = null, $type, $opt = null,$placeholder='')
    {
        $key = array('name' => $name, 'title' => $title, 'subtitle' => $subtitle, 'type' => $type, 'opt' => $opt,'placeholder'=>$placeholder);
        $this->_keyList[] = $key;
        return $this;
    }

    /** 图片展示 感觉更多的是进行头像展现
     * @param      $name
     * @param      $title
     * @param
     * @return AdminConfigBuilder
     */
    public function keyimg($name, $title)
    {
        return $this->key($name, $title, null, 'img');
    }


    /**
     * 密码字段
     * @param $name
     * @param $title
     * @return AdminConfigBuilder
     */
    public function keyPassword($name, $title)
    {
        return $this->key($name, $title, null, 'password');
    }

    public function keyWelcome(){
        return $this->key('','', null, 'welcome');
    }


    /**
     * 隐藏字段
     * @param $name
     * @return AdminConfigBuilder
     * @internal param $title
     * @internal param null $subtitle
     */
    public function keyHidden($name)
    {
        return $this->key($name, null,null , 'hidden');
    }

    /**
     * 隐藏字段 主要针对文件上传等 储存文件大小
     * @param $name
     * @return AdminConfigBuilder
     * @internal param $title
     * @internal param null $subtitle
     */
    public function KeyHiddenUploadFileSize($name)
    {
        return $this->key($name, null,null , 'hiddenUploadFileSize');
    }


    /**
     * 隐藏字段 主要针对于图片上传，获取图片上传大小
     * @param $name
     * @return AdminConfigBuilder
     */
    public function KeyHiddenUploadImageSize($name)
    {
        return $this->key($name, null,null , 'hiddenUploadImageSize');
    }



    /**
     * 只读文本
     * @param      $name
     * @param      $title
     * @param null $subtitle
     * @return AdminConfigBuilder
     */
    public function keyReadOnly($name, $title, $subtitle = null)
    {
        return $this->key($name, $title, $subtitle, 'readonly');
    }

    /**
     * 文本输入框
     * @param      $name
     * @param      $title
     * @param null $subtitle
     * @return AdminConfigBuilder
     */
    public function keyText($name, $title, $subtitle = null,$placeHolder='请输入内容')
    {
        return $this->key($name, $title, $subtitle, 'text',null,$placeHolder);
    }

    /**
     * textarea
     * @param $name
     * @param $title
     * @param null $subtitle
     * @return AdminConfigBuilder
     */
    public function keyTextArea($name, $title, $subtitle = null,$placeHolder='请输入内容')
    {
        return $this->key($name, $title, $subtitle, 'textarea',null,$placeHolder);
    }

    /**
     * 下拉选择
     * @param $name
     * @param $title
     * @param null $subtitle
     * @param $options
     * @return AdminConfigBuilder
     */
    public function keySelect($name, $title, $subtitle = null, $options)
    {
        return $this->key($name, $title, $subtitle, 'select', $options);
    }

    /**
     * 单选
     * @param $name
     * @param $title
     * @param null $subtitle
     * @param $options
     * @return AdminConfigBuilder
     */
    public function keyRadio($name, $title, $subtitle = null, $options)
    {
        return $this->key($name, $title, $subtitle, 'radio', $options);
    }

    /**
     * 多选框
     * @param $name
     * @param $title
     * @param null $subtitle
     * @param $options
     * @return AdminConfigBuilder
     */
    public function keyCheckBox($name, $title, $subtitle = null, $options)
    {
        return $this->key($name, $title, $subtitle, 'checkbox', $options);
    }

    /**
     * 富文本编辑器
     * @param $name
     * @param $title
     * @param null $subtitle
     * @return $this
     * @internal param string $config
     * @internal param array $style
     */
    public function keyEditor($name, $title, $subtitle = null)
    {
        $key = array('name' => $name, 'title' => $title, 'subtitle' => $subtitle, 'type' => 'editor');
        $this->_keyList[] = $key;
        return $this;
    }

    /**
     * 日期选择器：支持三种类型
     * @param $name
     * @param $title
     * @param null $subtitle
     * @param string $type 类型：支持（time）（datetime，默认）(date)
     * @return $this
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function keyTime($name, $title, $subtitle = null, $type = 'datetime')
    {
        return $this->key($name, $title, $subtitle, $type);
    }

    /**
     * boolean
     * @param $name
     * @param $title
     * @param null $subtitle
     * @return AdminConfigBuilder
     */
    public function keyBool($name, $title, $subtitle = null)
    {
        $map = array(1 => lang('_YES_'), 0 => lang('_NO_'));
        return $this->keyRadio($name, $title, $subtitle, $map);
    }

    /**单文件上传
     * @param $name
     * @param $title
     * @param null $subtitle
     * @return AdminConfigBuilder
     */
    public function keySingleFile($name, $title, $subtitle = null)
    {
        $id = substr(md5(time() . $title), 3, 8); //生成唯一id
        return $this->key($name, $title, $subtitle, 'singleFile',$id);
    }

//    /**多文件上传
//     * @param $name
//     * @param $title
//     * @param null $subtitle
//     * @return AdminConfigBuilder
//     */
//    public function keyMultiFile($name, $title, $subtitle = null)
//    {
//        return $this->key($name, $title, $subtitle, 'multiFile');
//    }

    public function keySingleImage($name, $title, $subtitle = null)
    {
        $id = substr(md5(time() . $title), 2, 7); //生成唯一id
        return $this->key($name, $title, $subtitle, 'singleImage',$id);
    }

//    public function keyMultiImage($name, $title, $subtitle = null, $limit = '')
//    {
//        return $this->key($name, $title, $subtitle, 'multiImage', $limit);
//    }

    public function button($title, $attr = array())
    {
        $this->_buttonList[] = array('title' => $title, 'attr' => $attr);
        return $this;
    }

    public function buttonSubmit($url = '', $title = '确定')
    {
        $this->savePostUrl($url);
        $attr = array();
        $attr['class'] = "btn submit-btn ajax-form btn-success  btn-right";
        $attr['id'] = 'submit';
        $attr['type'] = 'submit';
        $attr['target-form'] = 'form-horizontal';
        return $this->button($title, $attr);
    }

    public function buttonBack($title = '返回')
    {
        $attr = array();
        $attr['onclick'] = 'javascript:history.back(-1);return false;';
        $attr['class'] = 'btn btn-return btn-right';
        return $this->button($title, $attr);
    }

    public function buttonLink($title = '按钮', $attr)
    {
        $attr['onclick'] = 'javascript:location.href=\'' . $attr['href'] . '\';return false;';
        $attr['class'] ='btn-right';
        return $this->button($title, $attr);
    }

    public function data($list)
    {
        $this->_data = $list;
        return $this;
    }

    //传入url必须是url方法解决的
    private function savePostUrl($url)
    {
        if (empty($url)) {
            $this->_savePostUrl=$this->request->baseUrl();
        }else{
            $this->_savePostUrl = $url;
        }
    }

    public function displayConfig()
    {
        //将数据融入到key中
        foreach ($this->_keyList as &$e) {
            if ($e['type'] == 'multiInput') {
                $e['name'] = explode('|', $e['name']);
            }

            //修正在编辑信息时无法正常显示已经保存的地区信息/***修改的代码****/
            if (is_array($e['name'])) {
                $i = 0;
                $n = count($e['name']);
                while ($n > 0) {
                    $e['value'][$i] = $this->_data[$e['name'][$i]];
                    $i++;
                    $n--;
                }
            } else {
               if(isset($this->_data[$e['name']])){
                   $e['value'] = $this->_data[$e['name']];
               }else{
                   $e['value']='';
               }
            }
            //原代码
            /*$e['value'] = $this->_data[$e['name']];*/
        }

        //编译按钮的html属性
        foreach ($this->_buttonList as &$button) {
            $button['attr'] = $this->compileHtmlAttr($button['attr']);
        }

        //显示页面
        $this->assign('group', $this->_group);
        $this->assign('title', $this->_title);
        $this->assign('formsubmit', $this->_formsubmit);
        $this->assign('suggest', $this->_suggest);
        $this->assign('keyList', $this->_keyList);
        $this->assign('buttonList', $this->_buttonList);
        $this->assign('savePostUrl', $this->_savePostUrl);
      return  parent::display('admin_config');
    }

    public function setDefault($data, $key, $value)
    {
        $data[$key] = $data[$key] != null ? $data[$key] : $value;
        return $data;
    }

    public function keyDefault($key, $value)
    {
        $data = $this->_data;
        $data[$key] = $data[$key] !== null ? $data[$key] : $value;
        $this->_data = $data;
        return $this;
    }




}